package com.yds.baseproject_java;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.appcompat.app.AppCompatActivity;

import com.itheima.view.BridgeWebView;

public class BridgeWebViewActivity extends AppCompatActivity {

    private BridgeWebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bridge_web_view);

        mWebView = findViewById(R.id.mWebView);
        findViewById(R.id.mBtn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initBridgeWebView(mWebView, "https://10.6.1.29:443/wms/web/html/app_board");
            }
        });
        findViewById(R.id.mBtn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initBridgeWebView(mWebView, "https://www.doubao.com/chat/5110582929510658");
//                initBridgeWebView(mWebView, "https://wsdp.hxks.com::443/wms/web/html/app_board");
            }
        });

        initBridgeWebView(mWebView, "https://10.6.1.29:443/wms/web/html/app_board");
    }

    public void initBridgeWebView(BridgeWebView bridgeWebView, String url) {
        WebSettings settings = bridgeWebView.getSettings();

        // JavaScript支持
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);

        // 缓存与存储
        settings.setDomStorageEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setAppCacheEnabled(true);
        String cachePath = bridgeWebView.getContext().getDir("cache", Context.MODE_PRIVATE).getPath();
        settings.setAppCachePath(cachePath);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);

        // 文件与网络
        settings.setAllowFileAccess(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            settings.setAllowFileAccessFromFileURLs(true);
            settings.setAllowUniversalAccessFromFileURLs(true);
        }

        // 支持缩放（可选）
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false); // 隐藏缩放按钮

        // 性能优化
        bridgeWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setEnableSmoothTransition(true);

        // 安全设置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
            WebView.setWebContentsDebuggingEnabled(true);
        }

        // 设置WebViewClient和WebChromeClient
        bridgeWebView.setWebViewClient(new BridgeWebViewClient());
        bridgeWebView.setWebChromeClient(new BridgeWebChromeClient());

        mWebView.loadUrl(url);//显示H5页面
    }

    // 自定义BridgeWebViewClient（示例）
    private static class BridgeWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 处理URL拦截逻辑（如JS Bridge通信）
            if (url.startsWith("jsbridge://")) {
                handleBridgeUrl(url);
                return true;
            }
            return super.shouldOverrideUrlLoading(view, url);
        }

        private void handleBridgeUrl(String url) {
            // 解析并处理JS Bridge请求
        }
    }

    // 自定义BridgeWebChromeClient（示例）
    private static class BridgeWebChromeClient extends WebChromeClient {
        // 处理JS弹窗、进度条等
    }
}